package com.eleadin.ruleengine.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class IdGeneraterUtil {
    @Value("${guid.worker-id:0}")
    private int workerId;
    @Value("${guid.datacenter-id:0}")
    private int datacenterId;

    private volatile static SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取long类型的随机值
     * @return
     */
    public long nextId(){
        if(snowflakeIdWorker == null){
            synchronized (IdGeneraterUtil.class){
                if(snowflakeIdWorker == null){
                    snowflakeIdWorker = new SnowflakeIdWorker(workerId,datacenterId);
                }
            }
        }
        return snowflakeIdWorker.nextId();
    }

    /**
     * 获取String类型的随机值
     * @return
     */
    public String nextIdString(){
        return String.valueOf(nextId());
    }
}
